package phapp.action.qfd;

import java.io.*;
import java.util.*;

import javax.servlet.http.*;

import org.apache.struts2.ServletActionContext;

import net.sf.json.*;
import javacommon.util.*;
import javacommon.jquery.*;
import static javacommon.util.Struts2JsonHelper.*;

import com.opensymphony.xwork2.*;

import cn.org.rapid_framework.page.*;
import phapp.model.qfd.Qfd;
import javacommon.base.*;
import phapp.model.qfd.*;
import phapp.dao.qfd.*;
import phapp.service.qfd.impl.*;
import phapp.service.qfd.*;

/**
 * Title: t_qfd_data Description: 接收前面界面请求,通过json数据格式交互 Action Description:
 * 页面通过Ajax调用Action层,返回JSONO数据 Copyright: Copyright (c) 2012-10 Company:
 * 
 * @author you name
 * @version 1.0
 */

public class QfdAction extends BaseStruts2Action
{
   private File[] qfd_list_imgfile;
   private String[] qfd_list_imgfileFileName;
   private QfdManager qfdManager;

   public void setQfdManager(QfdManager manager)
   {
      this.qfdManager = manager;
   }

   public File[] getQfd_list_imgfile()
   {
      return qfd_list_imgfile;
   }

   public void setQfd_list_imgfile(File[] qfd_list_imgfile)
   {
      this.qfd_list_imgfile = qfd_list_imgfile;
   }

   public String[] getQfd_list_imgfileFileName()
   {
      return qfd_list_imgfileFileName;
   }

   public void setQfd_list_imgfileFileName(String[] qfd_list_imgfileFileName)
   {
      this.qfd_list_imgfileFileName = qfd_list_imgfileFileName;
   }

   /**
    * jqGrid使用列表 分页查询
    * 
    * @throws IOException
    * @author
    */
   public void list() throws IOException
   {
      PageRequest<Map> pr = JqueryPageHelper.createPageRequestForJquery(getRequest());
      Page page = qfdManager.findByPageRequest(pr);

      List<Qfd> Qfdlist = (List) page.getResult();
      ListRange<Qfd> resultList = new ListRange<Qfd>();
      resultList.setDataRows(Qfdlist);
      resultList.setRecords(page.getTotalCount());
      resultList.setRows(page.getPageSize());
      if(page.getTotalCount() % page.getPageSize() > 0)
      {
         resultList.setTotal(page.getTotalCount() / page.getPageSize() + 1);
      }
      else
      {
         resultList.setTotal(page.getTotalCount() / page.getPageSize());
      }
      resultList.setPage(pr.getPageNumber());
      outJson(resultList);
   }

   /**
    * Qfd添加
    * 
    * @author
    * @throws IOException
    */
   public void save() throws IOException
   {
      Map<String, Object> result = new HashMap<String, Object>();
      try
      {
         HttpServletRequest request = getRequest();
         String qfdJson = request.getParameter("qfdJson");
         if(null != qfdJson && !"".equals(qfdJson))
         {
            JSONObject jsonobject = JSONObject.fromObject(qfdJson);
            Qfd qfdobj = (Qfd) JSONObject.toBean(jsonobject, Qfd.class);
            qfdManager.save(qfdobj);
            result.put("success", true);
            result.put("msg", "添加成功!");
         }
         else
         {
            result.put("success", false);
            result.put("msg", "添加失败 数据错误!");
         }
      }
      catch(Exception e)
      {
         result.put("success", false);
         result.put("msg", "添加失败!");
         e.printStackTrace();
      }

      outJson(result);
   }

   /**
    * Qfd修改
    * 
    * @throws IOException
    * @author
    */
   public void update() throws IOException
   {
      Map<String, Object> result = new HashMap<String, Object>();
      try
      {
         HttpServletRequest request = getRequest();
         String qfdJson = request.getParameter("qfdJson");
         if(null != qfdJson && !"".equals(qfdJson))
         {
            JSONObject jsonobject = JSONObject.fromObject(qfdJson);
            Qfd qfdobj = (Qfd) JSONObject.toBean(jsonobject, Qfd.class);

            /** 设置主键值 */
            // qfdobj.setId(jsonobject.get("id").toString());
            qfdobj.setId(new Integer(jsonobject.get("id").toString()));

            qfdManager.update(qfdobj);
            result.put("success", true);
            result.put("msg", "修改成功!");
         }
         else
         {
            result.put("success", false);
            result.put("msg", "修改失败 数据错误!");
         }
      }
      catch(Exception e)
      {
         result.put("success", false);
         result.put("msg", "修改失败!");
         e.printStackTrace();
      }
      outJson(result);
   }

   /**
    * Qfd删除
    * 
    * @throws IOException
    * @author
    */
   public void delete() throws IOException
   {
      Map<String, Object> result = new HashMap<String, Object>();
      try
      {
         HttpServletRequest request = getRequest();
         String ids = request.getParameter("ids");
         if(null != ids && !"".equals(ids))
         {
            Qfd qfd = new Qfd();
            qfd.setIdList(StringTool.stringToArray(ids));
            qfdManager.removeByIdArry(qfd);
            result.put("success", true);
            result.put("msg", "删除成功");
         }
         else
         {
            result.put("success", false);
            result.put("msg", "删除失败 数据错误!");
         }
      }
      catch(Exception e)
      {
         result.put("success", false);
         result.put("msg", "删除失败!");
         e.printStackTrace();
      }
      outJson(result);
   }

   public void saveData() throws IOException
   {
      Map<String, Object> result = new HashMap<String, Object>();
      try
      {
         HttpServletRequest request = getRequest();
         String qfdJson = request.getParameter("qfd_list_req_param");
         if (null != qfdJson && qfdJson.length() > 0)
         {
            JSONObject jsonData = JSONObject.fromObject(qfdJson);
            Qfd qfdData = (Qfd) JSONObject.toBean(jsonData, Qfd.class);

            qfdData.setUploadFileSet(this.qfd_list_imgfile);
            qfdData.setUploadFileNameSet(this.qfd_list_imgfileFileName);

            if (jsonData.get("qfdId").toString() == null || jsonData.get("qfdId").toString().length() == 0)
            {
               qfdManager.saveData(qfdData, 0);
            }
            else
            {
               qfdManager.saveData(qfdData, 1);
            }

            result.put("success", true);
            result.put("msg", "操作成功！");
         }
         else
         {
            result.put("success", false);
            result.put("msg", "操作成功！");
         }
      }
      catch(Exception e)
      {
         result.put("success", false);
         result.put("msg", "添加失败!");
         e.printStackTrace();
      }
      finally
      {
         boolean bResult = (Boolean) result.get("success");
         HttpServletRequest httpRequest = (HttpServletRequest) ServletActionContext.getRequest();
         HttpServletResponse httpResponse = (HttpServletResponse) ServletActionContext.getResponse();
         String RedirectUrl = httpRequest.getContextPath() + 
            "/makeqfd/upload_result.jsp?error_code=" + (bResult ? 0 : 1);
         httpResponse.sendRedirect(RedirectUrl);
      }
      
      outJson(result);
   }
   
   
}
